/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import processing.core.PApplet;

/**
 *
 * @author Jhon
 *
 *
 * PILLESE EL METODO FIRTMAP QUE AHI ESTA TODO EL MAPEO DE LA ESCENA
 *
 *
 *
 */
public class Mapeo extends PApplet {

    private List<OurPoint> points;
    private List<OurLine> lines;
    private List<OurPolygon> poligons;
    private OurPoint init;
    private boolean move = false;
    private boolean paintPoints;
    private boolean paintLines;
    private boolean paintPolygons;
    private OurRectangle rec;
    private OurPoint[] paintRect;
    private static final int CLIP_IN = 0; // 0000
    private static final int CLIP_LEFT = 1;   // 0001
    private static final int CLIP_RIGHT = 2;  // 0010
    private static final int CLIP_DOWN = 4; // 0100
    private static final int CLIP_UP = 8;    // 1000
    private ArrayList<OurPoint> polyPoints = new ArrayList<OurPoint>();
    private boolean finishedPoligon = false;

    @Override
    public void setup() {
        size(600, 600);
        background(255, 255, 255);
        rec = new OurRectangle(300, 500, 100, 50);
        points = new ArrayList<OurPoint>();
        lines = new ArrayList<OurLine>();
        poligons = new ArrayList<OurPolygon>();
        paintPoints = false;
        paintLines = false;
    }

    @Override
    public void draw() {
        rec.update();
        background(255, 255, 255);
        line(0, 300, 600, 300);
        stroke(128,128,128);
        paintRect = rec.getPoints();
        for (int i = 1; i <= 3; i++) {
            line(paintRect[i - 1].getX(), paintRect[i - 1].getY(), paintRect[i].getX(), paintRect[i].getY());
        }
        line(paintRect[3].getX(), paintRect[3].getY(), paintRect[0].getX(), paintRect[0].getY());
        stroke(0,255,0);
        if (!points.isEmpty()) {
            for (OurPoint p : points) {
                point(p.getX(), p.getY());
            }
        }
        stroke(255,0,0);
        if (!lines.isEmpty()) {
            for (OurLine l : lines) {
                l.sort();
                line(l.getX1(), l.getY1(), l.getX2(), l.getY2());
            }
        }
        stroke(0,0,255);
        if (!poligons.isEmpty()) {
            //System.out.println("poligonos.size() "+poligons.size());
            for (OurPolygon p : poligons) {
                this.fillPolygon(p);
                //System.out.println("tam "+p.getPoints().size());
                //p.printMyPolygon();
                for (int i = 1; i < p.getPoints().size(); ++i) {
                    line(p.getPoints().get(i).getX(), p.getPoints().get(i).getY(), p.getPoints().get(i - 1).getX(), p.getPoints().get(i - 1).getY());
                }
                
            }
        }
        for (int i = 1; i < polyPoints.size(); ++i) {
            line(polyPoints.get(i).getX(), polyPoints.get(i).getY(), polyPoints.get(i - 1).getX(), polyPoints.get(i - 1).getY());
        }
        firstMap();
        stroke(0,0,0);
    }

    private void fillPolygon(OurPolygon p) {
        ArrayList<OurNode> aet = new ArrayList<OurNode>();
        for (int y = p.getMinY(); y <= p.getMaxY(); ++y) {
            for (int i = 0; i + 1 < aet.size(); i += 2) {
                for (int x = Math.round(aet.get(i).getxMin() - aet.get(i).getInv_m()); x <= Math.round(aet.get(i + 1).getxMin() - aet.get(i + 1).getInv_m()); x++) {
                    point(x, y);
                }
            }
            for (int i = 0; i < aet.size(); i++) {
                if (aet.get(i).getyMax() == y) {
                    aet.remove(i--);
                }
            }
            if (p.getEdgeTable().containsKey(y)) {
                for (int i = 0; i < p.getEdgeTable().get(y).size(); i++) {
                    aet.add(p.getEdgeTable().get(y).get(i).clone());
                }
            }
            for (int i = 0; i < aet.size(); i++) {
                aet.get(i).setXMin(aet.get(i).getxMin() + aet.get(i).getInv_m());
            }
            Collections.sort(aet);
            for (int i = 0; i + 1 < aet.size(); i += 2) {
                for (int x = Math.round(aet.get(i).getxMin() - aet.get(i).getInv_m()); x <= Math.round(aet.get(i + 1).getxMin() - aet.get(i + 1).getInv_m()); x++) {
                    point(x, y);
                }
            }
        }
    }

    @Override
    public void mousePressed() {
        if (mouseX >= 0 && mouseX <= 600 - rec.getWidth() && mouseY >= 300 && mouseY <= 600 - rec.getHeight()) {
            if (!move && isOnRec(mouseX, mouseY)) {
                move = true;
            } else if (paintPoints) {
                points.add(new OurPoint(mouseX, mouseY));
            } else if (paintLines) {
                init = new OurPoint(mouseX, mouseY);
            } else if (paintPolygons) {
                if (mouseButton == LEFT && !finishedPoligon) {
                    OurPoint point = new OurPoint(this.dmouseX, this.dmouseY);

                    polyPoints.add(point);
                    if (polyPoints.size() > 1) {
                        int x1 = polyPoints.get(polyPoints.size() - 1).getX();
                        int y1 = polyPoints.get(polyPoints.size() - 1).getY();
                        int x2 = polyPoints.get(polyPoints.size() - 2).getX();
                        int y2 = polyPoints.get(polyPoints.size() - 2).getY();
                        line(x1, y1, x2, y2);
                    }
                } else if (mouseButton == RIGHT && !finishedPoligon) {

                    //System.out.println("Polygon: ");
                    int x1 = polyPoints.get(polyPoints.size() - 1).getX();
                    int y1 = polyPoints.get(polyPoints.size() - 1).getY();
                    int x2 = polyPoints.get(0).getX();
                    int y2 = polyPoints.get(0).getY();
                    line(x1, y1, x2, y2);
                    OurPolygon polygon = new OurPolygon(polyPoints);
                    this.polyPoints = new ArrayList<OurPoint>();
                    //polygon.printMyPolygon();
                    this.poligons.add(polygon);
                    this.fillPolygon(polygon);
                    this.finishedPoligon = false;
                    polyPoints.clear();
                    //paintPolygons = false;
                } else {
                    finishedPoligon = true;
                }
            }
        }
    }

    @Override
    public void mouseDragged() {
        if (mouseX >= 0 && mouseX <= 600 - rec.getWidth() && mouseY >= 300 && mouseY <= 600 - rec.getHeight()) {
            if (move) {

                rec.setxPos(mouseX);
                rec.setyPos(mouseY);

            } else if (paintLines) {
                line(init.getX(), init.getY(), mouseX, mouseY);
            }
        }
    }

    @Override
    public void mouseReleased() {
        if (move) {
            move = false;
        } else if (paintLines) {
            if (mouseX >= 0 && mouseX <= 600 - rec.getWidth() && mouseY >= 300 && mouseY <= 600 - rec.getHeight()) {
                lines.add(new OurLine(init.getX(), init.getY(), mouseX, mouseY));
            }
        }
    }

    @Override
    public void keyPressed() {
        if (key == '+') {

            rec.setHeight(rec.getHeight() + 1);
            rec.setWidth(rec.getWidth() + 1);

        } else if (key == '-') {
            rec.setHeight(rec.getHeight() - 1);
            rec.setWidth(rec.getWidth() - 1);

        } else if (key == 'p') {
            paintPoints = true;
            paintLines = false;
            paintPolygons = false;
        } else if (key == 'l') {
            paintPoints = false;
            paintLines = true;
            paintPolygons = false;
        } else if (key == 'o') {
            paintPoints = false;
            paintLines = false;
            paintPolygons = true;
            //clickDerecho = false;

        } else if (key == '7') {
            rec.rotateMinusOneDegree();

        } else if (key == '9') {
            rec.rotateOneDegree();
        }
    }

    private int getClippingCode(int x, int y, int xmin, int xmax, int ymin, int ymax) {
        int code;
        code = CLIP_IN;          // initialised as being inside of clip window
        if (x < xmin) // to the left of clip window
        {
            code |= CLIP_LEFT;
        } else if (x > xmax) // to the right of clip window
        {
            code |= CLIP_RIGHT;
        }
        if (y < ymin) // below the clip window
        {
            code |= CLIP_DOWN;
        } else if (y > ymax) // above the clip window
        {
            code |= CLIP_UP;
        }

        return code;
    }

    public void firstMap() {
        int x, y, x1, y1, auxx0, auxy0, auxx1, auxy1;
        OurPoint point;
        stroke(0,0,255);
        if (!points.isEmpty()) {
            for (OurPoint p : points) {
                if (isOnRec(p.getX(), p.getY())) {
                    // aca trasladamos todos los Points que esten dentro del rectangulo...
                    // eso se sabe con la funcion isOnRec... ahi le retorna true/false si esta o no
                    
                   point = Our2DTransform.trasladarPoint(p.getX(), p.getY(), - rec.getxPos(),  - rec.getyPos());
                    x = point.getX();
                    y = point.getY();
                    //esta operacion es de rotacion... como queremos rotar con respecto al centro del
                    //rectangulo entonces primero traslado al centro del rectangulo
                    // roto con respecto al angulo que este actualmente girado en rectangulo
                    // y vuelvo a trasladar...
                    int xp, yp;

                    
                    x -= rec.getWidth() / 2;
                    y -= rec.getHeight() / 2;

                    point = Our2DTransform.rotarPoint(x, y, (float)-rec.getRadian());
                    xp = point.getX();
                    yp = point.getY();

                    xp += rec.getWidth() / 2;
                    yp += rec.getHeight() / 2;
                    // esta operacion es de escalar...
                    // como primero esta Traslado->Rotar->Escalar
                    point = Our2DTransform.escalarPoint(xp, yp, (float) 600 / (float) rec.getWidth(),  (float) 300 / (float) rec.getHeight());
                    x = point.getX();
                    y = point.getY();
                    point(x,y);

                    /*
                    int xp,yp;
                    
                    //esta operacion es de rotacion... como queremos rotar con respecto al centro del
                    //rectangulo entonces primero traslado al centro del rectangulo
                    // roto con respecto al angulo que este actualmente girado en rectangulo
                    // y vuelvo a trasladar...
//                    int xp, yp;
                    point = Our2DTransform.trasladarPoint(x, y, - rec.getxPos() / 2,  - rec.getyPos()/2);
                    xp = point.getX();
                    yp = point.getY();
                    
                    point = Our2DTransform.trasladarPoint(x, y,  rec.getxPos() / 2,   rec.getyPos()/2);
                    xp = point.getX();
                    yp = point.getY();
                    // esta operacion es de escalar...
                    // como primero esta Traslado->Rotar->Escalar
                    point = Our2DTransform.escalarPoint(xp, yp, (float) 600 / (float) rec.getWidth(),  (float) 300 / (float) rec.getHeight());
                    x = point.getX();
                    y = point.getY();
                    point(x, y);*/
                }
            }
        }

        stroke(0,255,0);
        if (!lines.isEmpty()) {
            for (OurLine l : lines) {
                point = Our2DTransform.trasladarPoint(l.getX1(), l.getY1(), - rec.getxPos(),  - rec.getyPos());
                    x = point.getX();
                    y = point.getY();
//                x = l.getX1() - rec.getxPos();
//                y = l.getY1() - rec.getyPos();
                point = Our2DTransform.trasladarPoint(l.getX2(), l.getY2(), - rec.getxPos(),  - rec.getyPos());
                    x1 = point.getX();
                    y1 = point.getY();
//                x1 = l.getX2() - rec.getxPos();
//                y1 = l.getY2() - rec.getyPos();
                //aqui hago lo mismo que hice con un OurPoint... pero ahora con los 2 Points q definen la linea
                x -= rec.getWidth() / 2;
                y -= rec.getHeight() / 2;
                x1 -= rec.getWidth() / 2;
                y1 -= rec.getHeight() / 2;
                int xp, x1p, yp, y1p;
                point = Our2DTransform.rotarPoint(x, y, (float)-rec.getRadian());
                    xp = point.getX();
                    yp = point.getY();
                point = Our2DTransform.rotarPoint(x1, y1, (float)-rec.getRadian());
                    x1p = point.getX();
                    y1p = point.getY();
                xp += rec.getWidth() / 2;
                yp += rec.getHeight() / 2;
                x1p += rec.getWidth() / 2;
                y1p += rec.getHeight() / 2;

                point = Our2DTransform.escalarPoint(xp, yp, (float) 600 / (float) rec.getWidth(),  (float) 300 / (float) rec.getHeight());
                    x = point.getX();
                    y = point.getY();

                    point = Our2DTransform.escalarPoint(x1p, y1p, (float) 600 / (float) rec.getWidth(),  (float) 300 / (float) rec.getHeight());
                    x1 = point.getX();
                    y1 = point.getY();
//                xp *= Math.round((float) 600 / (float) rec.getWidth());
//                yp *= Math.round((float) 300 / (float) rec.getHeight());
//                x1p *= Math.round((float) 600 / (float) rec.getWidth());
//                y1p *= Math.round((float) 300 / (float) rec.getHeight());


                auxx0 = x;
                auxx1 = x1;
                auxy0 = y;
                auxy1 = y1;
                boolean c = true;
                int outcode0 = getClippingCode(auxx0, auxy0, 0, 600, 0, 300);
                int outcode1 = getClippingCode(auxx1, auxy1, 0, 600, 0, 300);
                boolean accept = false;
                while (true) {
//                System.out.println(outcode0+" "+outcode1);
                    if ((outcode0 | outcode1) == 0) {     //logical or is 0. Trivially accept and get out of loop
                        accept = true;
                        break;
                    } else if ((outcode0 & outcode1) != 0) { //logical and is not 0. Trivially reject and get out of loop
                        break;
                    } else {
                        // failed both tests, so calculate the line segment to clip
                        // from an outside point to an intersection with clip edge
                        double xx = 0, yy = 0;

                        // At least one endpoint is outside the clip rectangle; pick it.
                        int outcodeOut = outcode0 != 0 ? outcode0 : outcode1;

//                        System.out.println(outcodeOut);

                        // Now find the intersection point;
                        // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
                        if ((outcodeOut & CLIP_UP) != 0) {
                            if (auxy1 != auxy0) {
                                xx = auxx0 + (auxx1 - auxx0) * (300 - auxy0) / (auxy1 - auxy0);
                            } else {
                                xx = auxx0;
                            }
                            yy = 300;// point is above the clip rectangle
                        } else if ((outcodeOut & CLIP_DOWN) != 0) { // point is below the clip rectangle
                            if (auxy1 != auxy0) {
                                xx = auxx0 + (auxx1 - auxx0) * (0 - auxy0) / (auxy1 - auxy0);
                            } else {
                                xx = auxx0;
                            }
                            yy = 0;
                        } else if ((outcodeOut & CLIP_RIGHT) != 0) {  // point is to the right of clip rectangle
                            if (auxx1 != auxx0) {
                                yy = auxy0 + (auxy1 - auxy0) * (600 - auxx0) / (auxx1 - auxx0);
                            } else {
                                yy = auxy0;
                            }
                            xx = 600;
                        } else if ((outcodeOut & CLIP_LEFT) != 0) {   // point is to the left of clip rectangle
                            if (auxx1 != auxx0) {
                                yy = auxy0 + (auxy1 - auxy0) * (0 - auxx0) / (auxx1 - auxx0);
                            } else {
                                yy = auxy0;
                            }
                            xx = 0;
                        }


                        // Now we move outside point to intersection point to clip
                        // and get ready for next pass.
                        if (outcodeOut == outcode0) {
                            auxx0 = (int) xx;
                            auxy0 = (int) yy;
                            outcode0 = getClippingCode(auxx0, auxy0, 0, 600, 0, 300);
                        } else {
                            auxx1 = (int) xx;
                            auxy1 = (int) yy;
                            outcode1 = getClippingCode(auxx1, auxy1, 0, 600, 0, 300);
                        }
                    }
                }
                if (accept) {
                    line(auxx0, auxy0, auxx1, auxy1);
                }

            }
        }
        stroke(255,0,0);
        int xpol, ypol;
        int [] valores;
        if (!poligons.isEmpty()) {
            //System.out.println("poligonos.size() "+poligons.size());
            for (OurPolygon p : poligons) {
                ArrayList<OurPoint> points = new ArrayList<OurPoint>();
                for(OurPoint po : p.getPoints()){
                    point = Our2DTransform.trasladarPoint(po.getX(), po.getY(), - rec.getxPos(),  - rec.getyPos());
                    xpol = point.getX();
                    ypol = point.getY();
                    //esta operacion es de rotacion... como queremos rotar con respecto al centro del
                    //rectangulo entonces primero traslado al centro del rectangulo
                    // roto con respecto al angulo que este actualmente girado en rectangulo
                    // y vuelvo a trasladar...
                    int xp, yp;
                    xpol -= rec.getWidth() / 2;
                    ypol -= rec.getHeight() / 2;
                    point = Our2DTransform.rotarPoint(xpol, ypol, (float)-rec.getRadian());
                    xp = point.getX();
                    yp = point.getY();
                    xp += rec.getWidth() / 2;
                    yp += rec.getHeight() / 2;
                    // esta operacion es de escalar...
                    // como primero esta Traslado->Rotar->Escalar
                    point = Our2DTransform.escalarPoint(xp, yp, (float) 600 / (float) rec.getWidth(),  (float) 300 / (float) rec.getHeight());
                    xpol = point.getX();
                    ypol = point.getY();
                    points.add(new OurPoint(xpol,ypol));
                }
                OurPolygon aux = new OurPolygon(points);
                valores = aux.getMaxYPoint();
                ArrayList<OurPoint> points2 = new ArrayList<OurPoint>();
                if(valores[0]>=0 && valores[1]>=0 && valores[2]<=600 && valores[3]<=300){
                    points = aux.getPoints();


                    for(int i = 1;i<points.size();i++){
                        if(points.get(i-1).getY()<=300 && points.get(i).getY()<=300){
                            points2.add(points.get(i));
                        }else if(points.get(i-1).getY()<=300 && points.get(i).getY()>300){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(i-1).getX(),points.get(i-1).getY(),points.get(i).getX(),points.get(i).getY()),new OurLine(0,300,600,300));
                            points2.add(auxiliar);
                        }else if(points.get(i-1).getY()>300 && points.get(i).getY()<=300){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(i-1).getX(),points.get(i-1).getY(),points.get(i).getX(),points.get(i).getY()),new OurLine(0,300,600,300));
                            points2.add(auxiliar);
                            points2.add(points.get(i));
                        }
                    }

                    if(points.get(points.size()-1).getY()<=300 && points.get(0).getY()<=300){
                            points2.add(points.get(0));
                        }else if(points.get(points.size()-1).getY()<=300 && points.get(0).getY()>300){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(points.size()-1).getX(),points.get(points.size()-1).getY(),points.get(0).getX(),points.get(0).getY()),new OurLine(0,300,600,300));
                            points2.add(auxiliar);
                        }else if(points.get(points.size()-1).getY()>300 && points.get(0).getY()<=300){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(points.size()-1).getX(),points.get(points.size()-1).getY(),points.get(0).getX(),points.get(0).getY()),new OurLine(0,300,600,300));
                            points2.add(auxiliar);
                            points2.add(points.get(0));
                        }
                    points.clear();

                    for(int i = 1;i<points2.size();i++){
                        if(points2.get(i-1).getY()>=0 && points2.get(i).getY()>=0){
                            points.add(points2.get(i));
                        }else if(points2.get(i-1).getY()>=0 && points2.get(i).getY()<0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(i-1).getX(),points2.get(i-1).getY(),points2.get(i).getX(),points2.get(i).getY()),new OurLine(0,0,600,0));
                            points.add(auxiliar);
                        }else if(points2.get(i-1).getY()<0 && points2.get(i).getY()>=0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(i-1).getX(),points2.get(i-1).getY(),points2.get(i).getX(),points2.get(i).getY()),new OurLine(0,0,600,0));
                            points.add(auxiliar);
                            points.add(points2.get(i));
                        }
                    }

                    if(points2.get(points2.size()-1).getY()>=0 && points2.get(0).getY()>=0){
                            points.add(points2.get(0));
                        }else if(points2.get(points2.size()-1).getY()>=0 && points2.get(0).getY()<0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(points2.size()-1).getX(),points2.get(points2.size()-1).getY(),points2.get(0).getX(),points2.get(0).getY()),new OurLine(0,0,600,0));
                            points.add(auxiliar);
                        }else if(points2.get(points2.size()-1).getY()<0 && points2.get(0).getY()>=0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(points2.size()-1).getX(),points2.get(points2.size()-1).getY(),points2.get(0).getX(),points2.get(0).getY()),new OurLine(0,0,600,0));
                            points.add(auxiliar);
                            points.add(points2.get(0));
                        }


                    points2.clear();




                    for(int i = 1;i<points.size();i++){
                        if(points.get(i-1).getX()<=600 && points.get(i).getX()<=600){
                            points2.add(points.get(i));
                        }else if(points.get(i-1).getX()<=600 && points.get(i).getX()>600){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(i-1).getX(),points.get(i-1).getY(),points.get(i).getX(),points.get(i).getY()),new OurLine(600,0,600,300));
                            points2.add(auxiliar);
                        }else if(points.get(i-1).getX()>600 && points.get(i).getX()<=600){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(i-1).getX(),points.get(i-1).getY(),points.get(i).getX(),points.get(i).getY()),new OurLine(600,0,600,300));
                            points2.add(auxiliar);
                            points2.add(points.get(i));
                        }
                    }

                    if(points.get(points.size()-1).getX()<=600 && points.get(0).getY()<=600){
                            points2.add(points.get(0));
                        }else if(points.get(points.size()-1).getX()<=600 && points.get(0).getY()>600){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(points.size()-1).getX(),points.get(points.size()-1).getY(),points.get(0).getX(),points.get(0).getY()),new OurLine(600,0,600,300));
                            points2.add(auxiliar);
                        }else if(points.get(points.size()-1).getX()>600 && points.get(0).getY()<=600){
                            OurPoint auxiliar =GetIntersection(new OurLine(points.get(points.size()-1).getX(),points.get(points.size()-1).getY(),points.get(0).getX(),points.get(0).getY()),new OurLine(600,0,600,300));
                            points2.add(auxiliar);
                            points2.add(points.get(0));
                        }
                    points.clear();

                    for(int i = 1;i<points2.size();i++){
                        if(points2.get(i-1).getX()>=0 && points2.get(i).getX()>=0){
                            points.add(points2.get(i));
                        }else if(points2.get(i-1).getX()>=0 && points2.get(i).getX()<0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(i-1).getX(),points2.get(i-1).getY(),points2.get(i).getX(),points2.get(i).getY()),new OurLine(0,0,0,300));
                            points.add(auxiliar);
                        }else if(points2.get(i-1).getX()<0 && points2.get(i).getX()>=0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(i-1).getX(),points2.get(i-1).getY(),points2.get(i).getX(),points2.get(i).getY()),new OurLine(0,0,0,300));
                            points.add(auxiliar);
                            points.add(points2.get(i));
                        }
                    }

                    if(points2.get(points2.size()-1).getY()>=0 && points2.get(0).getY()>=0){
                            points.add(points2.get(0));
                        }else if(points2.get(points2.size()-1).getY()>=0 && points2.get(0).getY()<0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(points2.size()-1).getX(),points2.get(points2.size()-1).getY(),points2.get(0).getX(),points2.get(0).getY()),new OurLine(0,0,0,300));
                            points.add(auxiliar);
                        }else if(points2.get(points2.size()-1).getY()<0 && points2.get(0).getY()>=0){
                            OurPoint auxiliar =GetIntersection(new OurLine(points2.get(points2.size()-1).getX(),points2.get(points2.size()-1).getY(),points2.get(0).getX(),points2.get(0).getY()),new OurLine(0,0,0,300));
                            points.add(auxiliar);
                            points.add(points2.get(0));
                        }


                    points2.clear();

                    aux = new OurPolygon(points);
                    this.fillPolygon(aux);


                }
            }
        }
    }


    public OurPoint GetIntersection(OurLine l1, OurLine l2)
    {
        double A1 = l1.getY2() - l1.getY1();
        double B1 = l1.getX1() - l1.getX2();
        double C1 = A1 * l1.getX1() + B1 * l1.getY1();
        double A2 = l2.getY2() - l2.getY1();
        double B2 = l2.getX1() - l2.getX2();
        double C2 = A2 * l2.getX1() + B2 * l2.getY1();
        double det = A1 * B2 - A2 * B1;
        if(det == 0)
        {
            if ((l1.getX1() == l2.getX1() && l1.getY1() == l2.getY1()) || (l1.getX1() == l2.getX2() && l1.getY1() == l2.getY2()))
            {
                return new OurPoint(l1.getX1(), l1.getY1());
            }
            if ((l1.getX2() == l2.getX1() && l1.getY2() == l2.getY1()) || (l1.getX2() == l2.getX2() && l1.getY2() == l2.getY2()))
            {
                return new OurPoint(l1.getX2(), l1.getY2());
            }
            return null;
        }
        double x = (B2 * C1 - B1 * C2) / det;
        double y = (A1 * C2 - A2 * C1) / det;
        return new OurPoint((int)Math.round(x), (int)Math.round(y));
    }

    public boolean isOnRec(int x, int y) {
        if (x >= rec.getxPos() && x <= (rec.getxPos() + rec.getWidth()) && y >= rec.getyPos() && y <= (rec.getyPos() + rec.getHeight())) {
            return true;
        } else {
            return false;
        }

    }

    public static void main(String[] args) {
        PApplet.main(new String[]{"maping.Mapeo"});
    }
}
